package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @description: 师资管理相关实现类
 * @author: ajie
 * @date: 2024/1/16 08:59:51
 * @version: 1.0
 */
@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    CourseTeacherMapper courseTeacherMapper;
    @Autowired
    CourseBaseMapper courseBaseMapper;

    /**
     * @description: 根据课程id查询教师
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/1/16 9:03
     */
    @Override
    public List<CourseTeacher> queryCourseTeacherList(Long courseId) {

        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId);
        List<CourseTeacher> courseTeachers = courseTeacherMapper.selectList(queryWrapper);

        return courseTeachers;
    }

    /**
     * @description: 添加与修改教师
     * @params: [courseTeacher]
     * @return:
     * @author: ajie
     * @date: 2024/1/16 10:58
     */
    @Override
    public CourseTeacher saveCourseTeacher(Long companyId, CourseTeacher courseTeacher) {
        Long courseId = courseTeacher.getCourseId();
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (courseBase == null) {
            XueChengPlusException.cast("该课程不存在");
        }
        //只允许向机构自己的课程中添加、修改老师信息
        if (!courseBase.getCompanyId().equals(companyId)) {
            XueChengPlusException.cast("只允许向机构自己的课程中添加、修改老师信息");
        }
        Long teacherId = courseTeacher.getId();
        //传id即修改
        if (teacherId == null) {
            //插入
            courseTeacher.setCreateDate(LocalDateTime.now());
            int insert = courseTeacherMapper.insert(courseTeacher);
            if (insert <= 0) {
                XueChengPlusException.cast("保存教师信息失败");
            }
            return courseTeacherMapper.selectById(teacherId);

        } else {
            //更新
            int i = courseTeacherMapper.updateById(courseTeacher);
            if (i <= 0) {
                XueChengPlusException.cast("更新教师信息失败");
            }
            return courseTeacherMapper.selectById(teacherId);
        }

    }

    /**
     * @description: 删除课程教师
     * @params: [companyId, courseId, teacherId]       
     * @return:
     * @author: ajie
     * @date: 2024/1/16 13:29
     */
    @Override
    public void deleteCourseTeacher(Long companyId, Long courseId, Long teacherId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if (courseBase == null) {
            XueChengPlusException.cast("该课程不存在");
        }
        //校验机构id，只允许向机构自己的课程中删除老师信息
        if (!courseBase.getCompanyId().equals(companyId)) {
            XueChengPlusException.cast("只允许向机构自己的课程中删除老师信息");
        }
        //封装条件
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseId).eq(CourseTeacher::getId, teacherId);
        //删除
        int delete = courseTeacherMapper.delete(queryWrapper);
        if (delete <= 0) {
            XueChengPlusException.cast("删除课程老师失败");
        }
    }
}
